package com.emmmya.ocs.modules.system.serviceImpl;

import com.emmmya.ocs.modules.system.dao.UserDao;
import com.emmmya.ocs.modules.system.dao.mapper.UserRoleMapper;
import com.emmmya.ocs.modules.system.entity.User;
import com.emmmya.ocs.modules.system.entity.UserRole;
import com.emmmya.ocs.modules.system.service.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户角色接口实现
 * @author laijieguan
 */
@Slf4j
@Service
@Transactional
public class UserRoleServiceImpl implements UserRoleService {

//    @Autowired
//    private UserRoleDao userRoleDao;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private UserDao userDao;

    @Override
    public List<UserRole> findByRoleId(Long roleId) {
        return userRoleMapper.findByRoleId(roleId);
    }

    @Override
    public List<User> findUserByRoleId(Long roleId) {

        List<UserRole> userRoleList = userRoleMapper.findByRoleId(roleId);
        List<User> list = new ArrayList<>();
        for(UserRole ur : userRoleList){
            User u = userDao.findById(ur.getUserId()).orElse(null);
            if(u != null && "1".equals(u.getStatus())){
                list.add(u);
            }
        }
        return list;
    }

    @Override
    public void deleteByUserId(Long userId) {
        userRoleMapper.deleteByUserId(userId);
    }

    @Override
    public UserRole findByRoleIdAndUserId(Long roleId, Long userId) {
        return userRoleMapper.findByRoleIdAndUserId(roleId, userId);
    }

    @Override
    public List<UserRole> findByUserId(Long userId) {
        return userRoleMapper.findByUserId(userId);
    }
}
